package com.shenfa.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.City;
import com.shenfa.bean.Paper;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.bean.PaperInfo;
import com.shenfa.bean.TreeNode;
import com.shenfa.bean.TreeNodeCheck;
import com.shenfa.dao.PaperDao;
import com.shenfa.dao.ProductDao;
import com.shenfa.util.sequence.SequenceUtil;

public class PaperService {

	// 日志
	private static final Logger log = Logger.getLogger(RuleService.class);

	@Autowired
	private PaperDao paperDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private SequenceUtil sequenceUtil;
	/**
	 * 预印纸gridpanel列表生成
	 * 
	 * @param cardid
	 * @param skipResults
	 * @param maxResults
	 * @return
	 */
	public List<Paper> doGridList(String cardid, int skipResults,int maxResults) {
		List<Paper> list = paperDao.queryGridList(cardid, skipResults, maxResults);
		return list;
	}
	
	/**
	 * 查询翻页条数
	 * @param cardid
	 * @return
	 */
	public int queryCount(String cardid) {
		return  paperDao.queryCount(cardid);	
	}
	
	/**
	 * 根据有效期时段查询预印纸
	 * @param pcc
	 * @param pp
	 * @return
	 */
	public List<Paper> doPaperByTimeCard(Paper pp) {
		return paperDao.queryPaperByTimeCard(pp);
	}
	
	/**
	 * 预印刷纸从表
	 * 
	 * @param paperid
	 * @return
	 */
	public List<PaperInfo> doPaperInfoLsit(String paperid) {
		List<PaperInfo> list = paperDao.queryPaperInfo(paperid);
		return list;
	}
	
	/**
	 * 预印纸单个从表
	 * @param p
	 * @return
	 */
	public List<PaperInfo> doAPaperInfo(PaperInfo p) {
		List<PaperInfo> list = paperDao.queryAPaperInfo(p);
		return list;
	}
	
	/**
	 * 根据卡产品id查询城市集合
	 * @param cardid
	 * @return
	 */
	public List<TreeNodeCheck> cityByCardList(PaperCityCrd pcc) {
		List<TreeNode> list = new ArrayList<TreeNode>();
		List<TreeNodeCheck> cityCheckList = new ArrayList<TreeNodeCheck>();
		try{
			 list = paperDao.queryCityByCard(pcc);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		if(null!=list){			
			for (TreeNode tn: list) {
				TreeNodeCheck tnc = new TreeNodeCheck();
				tnc.setId(tn.getId());
				tnc.setLeaf(tn.isLeaf());
				tnc.setText(tn.getText());
				cityCheckList.add(tnc);
			}
		}
		return cityCheckList;
	}
	
	/**
	 * 根据卡产品id和预印刷纸id查询城市集合
	 * @param cardid
	 * @param printId
	 * @return
	 * @throws SQLException
	 */
	public List<TreeNodeCheck> cityByCardPrintList(PaperCityCrd pcc){
		List<TreeNode> list = new ArrayList<TreeNode>();
		try{
			 list = paperDao.queryCityByCardPrint(pcc);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		List<TreeNodeCheck> cityCheckList = new ArrayList<TreeNodeCheck>();
		for(TreeNode tn: list){
			TreeNodeCheck tnc = new TreeNodeCheck();
			tnc.setId(tn.getId());
			tnc.setLeaf(tn.isLeaf());
			tnc.setText(tn.getText());
			cityCheckList.add(tnc);
		}
		return cityCheckList;
	}
	
	/**
	 * 添加预印纸以及对应卡产品预印纸的关联信息
	 * @author zys(2011-09-15)
	 * @param p
	 * @param cardid
	 * @return 0:成功  -1:失败
	 */
	public int addP(Paper p, String cardid, PaperCityCrd t) {
		int num = paperDao.addPaper(p);
		if(num==0){
			String[] cityNos = t.getCity_no().split("\\|");
			PaperCityCrd pCC = new PaperCityCrd();
			pCC.setPaper_no(p.getPid());
			pCC.setCard_no(cardid);
			pCC.setBusinpnt_no(t.getBusinpnt_no());
			for (int i = 0; i < cityNos.length; i++) {
				pCC.setCity_no(cityNos[i]);
				Boolean b = productDao.addPaperByCardCity(pCC);
				if(!b){
					num = -1;
				}
			}				
		}		
		return num;
	}
	
	/**
	 * 修改更新预印纸
	 * @author zys(2011-09-15)
	 * @param p
	 * @return 0:成功  -1:失败
	 */
	public int udpP(Paper p, PaperCityCrd t) {
		int num = paperDao.updPaper(p);
		
		if(num==0){
			t.setPaper_no(p.getPid());
			int b1 = paperDao.delPaperRelative(t);//先删除预印纸与城市卡产品的关联关系
			if(b1==0){
				String[] cityNos = t.getCity_no().split("\\|");
				PaperCityCrd pCC = new PaperCityCrd();
				pCC.setPaper_no(p.getPid());
				pCC.setCard_no(t.getCard_no());
				pCC.setBusinpnt_no(t.getBusinpnt_no());
				for (int i = 0; i < cityNos.length; i++) {
					pCC.setCity_no(cityNos[i]);
					Boolean b = productDao.addPaperByCardCity(pCC);//添加新的预印纸与城市卡产品的关联关系
					if(!b){
						num = -1;
					}
				}	
			}else{
				num = -1;
			}
		}
		return num;
	}
	
	/**
	 * 解除指定卡产品预印纸对应外包商关联关系
	 * @author zys(2011-09-20)
	 * @param p
	 * @param cardid
	 * @return
	 */
	public int delP(PaperCityCrd t) {
		int flag = paperDao.delPaperRelative(t);
		if(flag == 0) {
			paperDao.delPaperInfo(t.getPaper_no());
			paperDao.delPaper(t.getPaper_no());
		}
		return flag;
	}	

	//获取预印纸序列号
	public String nextKey() {
		return sequenceUtil.getSequence(SequenceUtil.SEQ_BEFOR_PRINT_BASIC);
	}
	
	//验证预印纸编号
	public String checkPaperNo(String paperNo) {
		return paperDao.checkPaperNo(paperNo);
	}
	
	/**
	 * 添加预印纸信息从表
	 * @author zys(2011-09-15)
	 * @param pI
	 * @return 0成功，-1失败
	 */
	public int addPInfo(PaperInfo pI) {
		PaperInfo p = new PaperInfo();
		p.setBg_no(pI.getBg_no());
		p.setBg_name(pI.getBg_name()+"_F_"+(Integer.parseInt(pI.getBg_no())+1)/2);
		p.setBefor_type("1");
		p.setId(pI.getId());
		p.setFront("");
		int num = paperDao.addPaperInfo(p);
		if(num == 0){
			pI.setBg_no(String.valueOf(Integer.parseInt(pI.getBg_no())+1));
			pI.setBg_name(pI.getBg_name()+"_B_"+(Integer.parseInt(pI.getBg_no())+1)/2);
			pI.setBefor_type("2");
			pI.setFront("");
			int num1 = paperDao.addPaperInfo(pI);
			if(num1 != 0)
				return -1;
		}
		return 0;
	}
	
	/**
	 * 修改预印纸信息从表
	 * @author zys(2011-09-15)
	 * @param pI
	 * @return 0成功，-1失败
	 */
	public int udpPInfo(PaperInfo pI) {
		int num = paperDao.udpPaperInfo(pI);
		return num;
	}
	
	/**
	 * 单独操作删除预印纸信息从表
	 * @author zys(2011-09-15)
	 * @param paperId
	 * @return 0成功，-1失败
	 */
	public int delPInfo(PaperInfo pI) {
		int num = paperDao.delPaperInfoSingle(pI);
		if(num == 0){
			PaperInfo pI1 = new PaperInfo();
			int no = Integer.parseInt(pI.getBg_no());
			if(0==(no%2)){
				pI1.setBg_no(String.valueOf(no-1));
				pI1.setId(pI.getId());
				int num1 = paperDao.delPaperInfoSingle(pI1);
				if(num1!=0){
					num = -1;
				}
			}else{
				pI1.setBg_no(String.valueOf(no+1));
				pI1.setId(pI.getId());
				int num1 = paperDao.delPaperInfoSingle(pI1);
				if(num1!=0){
					num = -1;
				}
			}
		}
		return num;
	}
	
	/**
	 * 根据预印纸ID删除从表信息
	 */
	public int delPaperInfoById(String pid) {
		return paperDao.delPaperInfoById(pid);
	}
	
	/**
	 * 预览文件
	 * @param id
	 * @return
	 */
	public String show(String id) {
		return paperDao.show(id);
	}
}
